package com.haq.housesystem.ctrl;

import com.github.pagehelper.PageInfo;
import com.haq.housesystem.bean.Admins;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.haq.housesystem.service.AdminsService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admins")
public class AdminsCtrl {
    @Autowired
    AdminsService adminsService;

    @RequestMapping("/insert")
    public Map insert(Admins admins) {
        adminsService.insert(admins);
        Map map=new HashMap();
        map.put("state", "success");
        return map;
    }
    @RequestMapping("/update")
    public Map update(Admins admins) {
        adminsService.update(admins);
        Map map=new HashMap();
        map.put("state", "success");
        return map;
    }

    @RequestMapping("/delete")
    public Map delete(String adminsId) {
        adminsService.delete(adminsId);
        Map map=new HashMap();
        map.put("state", "success");
        return map;
    }

    @RequestMapping("/deleteByIds")
    public Map deleteByIds(String[] adminsIds) {
        adminsService.deleteByIds(adminsIds);
        Map map=new HashMap();
        map.put("state", "success");
        return map;
    }
    @RequestMapping("/selectAll")
    public Map selectAll() {
        List<Admins> list = adminsService.selectAll();
        Map map=new HashMap();
        map.put("state", "success");
        map.put("adminslist", list);
        return map;
    }
    @RequestMapping("/selectByNameState")
    public Map selectByNameState(String adminsName,@RequestParam(required=false,defaultValue="-1")int adminsState) {
        List<Admins> list = adminsService.selectByNameState(adminsName, adminsState);
        Map map=new HashMap();
        map.put("state", "success");
        map.put("adminslist", list);
        return map;
    }
    //登陆--selectById
    @RequestMapping("/checkLogin")
    public Map checkLogin(String adminsId,String adminsPwd) {
        Map map=new HashMap();
        Admins admins=adminsService.selectById(adminsId);
        if(admins!=null&&admins.getAdminsPwd().equals(adminsPwd)) {//登陆成功
            map.put("state", "success");
            map.put("admins", admins);
        }else {
            map.put("state", "error");
        }
        return map;
    }
    //分页查询全部
    @RequestMapping("/selectAllByPageInfo")
    public Map selectAllByPageInfo(@RequestParam(required=false,defaultValue="1")int curPage,@RequestParam(required=false,defaultValue="3")int size) {
        PageInfo<Admins> pageInfo = adminsService.selectAllByPageInfo(curPage, size);

        Map map=new HashMap();
        map.put("state", "success");
        //list数据
        map.put("adminslist", pageInfo.getList());
        //总记录数
        map.put("total", pageInfo.getTotal());
        return map;
    }
    @RequestMapping("/selectAllByNameStatePageInfo")
    public Map selectAllByNameStatePageInfo(@RequestParam(required=false,defaultValue="1")int curPage,@RequestParam(required=false,defaultValue="3")int size,String adminsName,@RequestParam(required=false,defaultValue="-1")int adminsState) {
        PageInfo<Admins> pageInfo = adminsService.selectByNameStateByPageInfo(curPage, size, adminsName, adminsState);

        Map map=new HashMap();
        map.put("state", "success");
        //list数据
        map.put("adminslist", pageInfo.getList());
        //总记录数
        map.put("total", pageInfo.getTotal());
        return map;
    }

}
